package com.jgp.third.pay.controller;

import com.jgp.sys.controller.JGPBackController;
import com.jgp.third.pay.model.PaymentConfig;
import com.jgp.third.pay.pojo.NormalBatchTransInfo;
import com.jgp.third.pay.pojo.NormalOrderInfo;
import com.jgp.third.pay.pojo.PayOverOrderInfo;
import com.jgp.third.pay.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.net.URL;
import java.util.Objects;

/**
 * 项目   jgp-cloud-parent
 * 作者   loufei
 * 时间   2018/11/13
 */
@Controller
@RequestMapping("/third/pay/payController")
public class PayController extends JGPBackController {
    
    @Autowired
    private PayService payService;
    
    @RequestMapping("enterPay")
    public String enterPay() {
        return page("enterPay");
    }
    
    @RequestMapping("doPay")
    public String doPay(RedirectAttributes attr, NormalOrderInfo orderInfo, String extParamMap, String
            callbackParamMap) {
        PaymentConfig config = payService.queryPaymentConfigById(orderInfo.getConfigId());
        attr.addFlashAttribute("orderInfo", orderInfo);
        attr.addFlashAttribute("extParamMap", extParamMap);
        attr.addFlashAttribute("callbackParamMap", callbackParamMap);
        attr.addFlashAttribute("paymentConfig", config);
        String redirectUrl = "";
        switch (config.getMode()) {
            case ALI:
                redirectUrl = "redirect:/third/pay/aliPayController/doPay";
                break;
        }
        return redirectUrl;
    }
    
    @RequestMapping("doBatchTrans")
    public String doBatchTrans(RedirectAttributes attr, NormalBatchTransInfo normalBatchTransInfo, String details, String callbackParamMap) {
        PaymentConfig config = payService.queryPaymentConfigById(normalBatchTransInfo.getConfigId());
        attr.addFlashAttribute("normalBatchTransInfo", normalBatchTransInfo);
        attr.addFlashAttribute("details", details);
        attr.addFlashAttribute("callbackParamMap", callbackParamMap);
        attr.addFlashAttribute("paymentConfig", config);
        String redirectUrl = "";
        switch (config.getMode()) {
            case ALI:
                redirectUrl = "redirect:/third/pay/aliPayController/doBatchTrans";
                break;
        }
        return redirectUrl;
    }
    
    @RequestMapping("payReturn")
    public String payReturn(Model model, @ModelAttribute("orderInfo") PayOverOrderInfo orderInfo){
        re(model,"orderInfo",orderInfo);
        URL url = this.getClass().getResource("/pay/payReturn.htm");
        if(Objects.nonNull(url)){
            return "pay/payReturn";
        }else return page("payReturn");
    }
    
}
